<?php

/**
城市切换页面 yjmp
*/

define('IN_ECS', true);
define('ECS_ADMIN', true);

include_once(dirname(__FILE__) . '/includes/init.php');

/*获取城市列表*/
if ($_REQUEST['act'] == 'list') {
	$city_list = $GLOBALS['db']->getAll('SELECT * FROM '.$GLOBALS['ecs']->table('region').' WHERE region_type = 2 ORDER BY convert(region_name using gbk) ASC ');

	$city_list_format = array();
	$pos = 0;
	foreach ($city_list as $k => $v) {
		$res = get_first_char($v['region_name']);
		$first_char  = empty($res) ? $city_list_format[$pos]['title'] : $res;
		
		if ($city_list_format[$pos]['title'] != $first_char) {
			$pos++;
		}
		if (empty($city_list_format[$pos])) {
			$city_list_format[$pos]['title'] = $first_char;
			$city_list_format[$pos]['content'] = array();
		}
		array_push($city_list_format[$pos]['content'], $v);
	}

	//定位城市 
	$city_id = !empty($_SESSION['pos_city_id'])? $_SESSION['pos_city_id'] : 0;
	$city_name = $GLOBALS['db']->getOne('SELECT region_name FROM '.$GLOBALS['ecs']->table('region').' WHERE region_id = "'.$city_id.'"');
	$smarty->assign('city_name',$city_name);

	//当前城市
	$crt_city_id = !empty($_SESSION['city_id'])? $_SESSION['city_id'] : 0;
	$crt_city_name = $GLOBALS['db']->getOne('SELECT region_name FROM '.$GLOBALS['ecs']->table('region').' WHERE region_id = "'.$crt_city_id.'"');
	$smarty->assign('crt_city_name',$crt_city_name);
	$smarty->assign('city_list_format',$city_list_format);
	$smarty->display('sel_city.dwt');
	exit;
}
/*选择城市*/
if ($_REQUEST['sel'] == 'city') {
	$city_id = isset($_REQUEST['id'])? $_REQUEST['id'] : 0;
	$_SESSION['city_id'] = $city_id;
	$city_name = $GLOBALS['db']->getOne('SELECT region_name FROM '.$GLOBALS['ecs']->table('region').' WHERE region_id = "'.$city_id.'"');
	$GLOBALS['smarty']->assign('city',$city_name);
	/*当前城市*/
	$crt_city = $GLOBALS['db']->getOne('SELECT region_name FROM '.$GLOBALS['ecs']->table('region').' WHERE region_id = "'.$_SESSION['city_id'].'"');
	$smarty->assign('crt_city_name',$crt_city);
	$smarty->assign('ctr_city_id',$_SESSION['city_id']);
	header('index.php');
}

/*获取县列表*/
if ($_REQUEST['act'] == 'sel_district') {
	$city_id = $_REQUEST['id'];
	$district_list = $GLOBALS['db']->getAll('SELECT * FROM '.$GLOBALS['ecs']->table('region').' WHERE region_type = 3 AND parent_id = "'.$city_id.'" ORDER BY convert(region_name using gbk) ASC ');
	$district_list_format = array();
	$pos = 0;
	foreach ($district_list as $k => $v) {
		$res = get_first_char($v['region_name']);
		$first_char  = empty($res) ? $district_list_format[$pos]['title'] : $res;
		
		if ($district_list_format[$pos]['title'] != $first_char) {
			$pos++;
		}
		if (empty($district_list_format[$pos])) {
			$district_list_format[$pos]['title'] = $first_char;
			$district_list_format[$pos]['content'] = array();
		}
		array_push($district_list_format[$pos]['content'], $v);
	}
	//当前地区
	$smarty->assign('district_list_format',$district_list_format);
	$smarty->assign('crt_district',$_SESSION['pos_district']);

	//当前城市
	$crt_city_id = !empty($_SESSION['city_id'])? $_SESSION['city_id'] : 0;
	$crt_city_name = $GLOBALS['db']->getOne('SELECT region_name FROM '.$GLOBALS['ecs']->table('region').' WHERE region_id = "'.$crt_city_id.'"');
	$smarty->assign('crt_city_name',$crt_city_name);

	$smarty->display('sel_district.dwt');exit;
	
}

/*选择县区*/
if ($_REQUEST['sel'] == 'district_done') {
	$dis_id = isset($_REQUEST['id'])? $_REQUEST['id'] : 0;
	$dis_name = $GLOBALS['db']->getOne('SELECT region_name FROM '.$GLOBALS['ecs']->table('region').' WHERE region_id = "'.$dis_id.'"');
	$GLOBALS['smarty']->assign('district',$dis_name);
	$_SESSION['district'] = $dis_name;
	header('index.php');
}

//输入汉语词，返回第一个字的首字母
function get_first_char($str)
{
	if(empty($str)){return '';}  
	$fchar=ord($str{0});
	if($fchar>=ord('A')&&$fchar<=ord('z')) return strtoupper($str{0});  
	$s1=iconv('UTF-8','gb2312',$str);  
	$s2=iconv('gb2312','UTF-8',$s1);  
	$s=$s2==$str?$s1:$str;  
	$asc=ord($s{0})*256+ord($s{1})-65536;  
	if($asc>=-20319&&$asc<=-20284) return 'A';  
	if($asc>=-20283&&$asc<=-19776) return 'B';  
	if($asc>=-19775&&$asc<=-19219) return 'C';  
	if($asc>=-19218&&$asc<=-18711) return 'D';  
	if($asc>=-18710&&$asc<=-18527) return 'E';  
	if($asc>=-18526&&$asc<=-18240) return 'F';  
	if($asc>=-18239&&$asc<=-17923) return 'G';  
	if($asc>=-17922&&$asc<=-17418) return 'H';  
	if($asc>=-17417&&$asc<=-16475) return 'J';  
	if($asc>=-16474&&$asc<=-16213) return 'K';  
	if($asc>=-16212&&$asc<=-15641) return 'L';  
	if($asc>=-15640&&$asc<=-15166) return 'M';  
	if($asc>=-15165&&$asc<=-14923) return 'N';  
	if($asc>=-14922&&$asc<=-14915) return 'O';  
	if($asc>=-14914&&$asc<=-14631) return 'P';  
	if($asc>=-14630&&$asc<=-14150) return 'Q';  
	if($asc>=-14149&&$asc<=-14091) return 'R';  
	if($asc>=-14090&&$asc<=-13319) return 'S';  
	if($asc>=-13318&&$asc<=-12839) return 'T';  
	if($asc>=-12838&&$asc<=-12557) return 'W';  
	if($asc>=-12556&&$asc<=-11848) return 'X';  
	if($asc>=-11847&&$asc<=-11056) return 'Y';  
	if($asc>=-11055&&$asc<=-10247) return 'Z';  
	return null;  
}

?>	